home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / C⁄C++ OS8 / AMReminder / MainMenu.cp < prev    next >
Encoding:
Text File  |  1998-10-17  |  5.0 KB  |  254 lines  |  [TEXT/CWIE]

  1. /* MainMenu.cp */
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>
  7. #include <Events.h>
  8. #include <Lists.h>
  9. #include <LowMem.h>
  10. #include <Menus.h>
  11. #include <TextEdit.h>
  12. #include <ToolUtils.h>
  13.  
  14. #include "Globals.h"
  15. #include "ResourceDefs.h"
  16. #include "Miscellany.h"
  17. #include "AMApp.h"
  18. #include "AMDoc.h"
  19. #include "AMEngine.h"
  20. #include "AMWindow.h"
  21. #include "MainMenu.h"
  22.  
  23.  
  24. /*----------*/
  25. static long        GetCommandFromMenu (long        menuChoice);
  26. static void        DoApple            (short            itemNr);
  27. static void        Enable            (short            itemNr,
  28.                                  Boolean        enabled);
  29. static void        EnableTitle        (MenuHandle        menu,
  30.                                  Boolean        enabled);
  31.  
  32. /*----------*/
  33. void    InitTitles (void)
  34. {
  35. } /*InitTitles*/
  36.  
  37. /*----------*/
  38. void    LoadMenus (void)
  39. {
  40.     AppleMenu    = GetMenu (MENU_Apple);
  41.     FailNilResource ((Handle)AppleMenu);
  42.     AppendResMenu (AppleMenu, 'DRVR');
  43.     FileMenu    = GetMenu (MENU_File);
  44.     EditMenu    = GetMenu (MENU_Edit);
  45.     RemindMenu    = GetMenu (MENU_Remind);
  46.  
  47.     InsertMenu (AppleMenu, 0);
  48.     InsertMenu (FileMenu, 0);
  49.     InsertMenu (EditMenu, 0);
  50.     InsertMenu (RemindMenu, 0);
  51.  
  52.     DrawMenuBar ();
  53. } /*LoadMenus*/
  54.  
  55. //----------
  56. long    GetCommandFromMenu (
  57.     long        menuChoice)
  58. {
  59.     long        commandID = 0;
  60.  
  61.     switch (menuChoice) {
  62.         case cAppleAbout:
  63.                 commandID = cmdAbout;
  64.             break;
  65.         case cFileNew:
  66.                 commandID = cmdNew;
  67.             break;
  68.         case cFileOpen:
  69.                 commandID = cmdOpen;
  70.             break;
  71.         case cFileClose:
  72.                 commandID = cmdClose;
  73.             break;
  74.         case cFileSave:
  75.                 commandID = cmdSave;
  76.             break;
  77.         case cFileSaveAs:
  78.                 commandID = cmdSaveAs;
  79.             break;
  80.         case cFileRevert:
  81.                 commandID = cmdRevert;
  82.             break;
  83.         case cFilePageSetup:
  84.                 commandID = cmdPageSetup;
  85.             break;
  86.         case cFilePrint:
  87.                 commandID = cmdPrint;
  88.             break;
  89.         case cFileQuit:
  90.                 commandID = cmdQuit;
  91.             break;
  92.         case cEditUndo:
  93.                 commandID = cmdUndo;
  94.             break;
  95.         case cEditCut:
  96.                 commandID = cmdCut;
  97.             break;
  98.         case cEditCopy:
  99.                 commandID = cmdCopy;
  100.             break;
  101.         case cEditPaste:
  102.                 commandID = cmdPaste;
  103.             break;
  104.         case cEditClear:
  105.                 commandID = cmdClear;
  106.             break;
  107.         case cEditSelectAll:
  108.                 commandID = cmdSelectAll;
  109.             break;
  110.         case cEditShowClipboard:
  111.                 commandID = cmdShowClipboard;
  112.             break;
  113.  
  114.         default:
  115.                 commandID = -menuChoice;
  116.     }
  117.  
  118.     return commandID;
  119. }
  120.  
  121. //----------
  122. void    DoApple (
  123.     short        itemNr)
  124. {
  125.     Str255            name;
  126.     short            refNum;
  127.  
  128.     GetMenuItemText (AppleMenu, itemNr, name);
  129.     refNum = OpenDeskAcc (name);
  130. }
  131.  
  132. /*----------*/
  133. void    DoMenu (
  134.     long        menuChoice)
  135. {
  136.     long            commandID;
  137.     AMDoc*            curDoc;
  138.     short            menuID;
  139.     short            itemNr;
  140.  
  141.     commandID = GetCommandFromMenu (menuChoice);
  142.     curDoc = cur->mDoc;
  143.     if (cur->DoCommand (commandID)) {
  144.         // cur window handled it
  145.     } else if ((curDoc != nil)
  146.     && (curDoc->DoCommand (commandID))) {
  147.         // document handled it
  148.     } else if (gApplication->DoCommand (commandID)) {
  149.         // application handled it
  150.     } else {
  151.         menuID = HiWord (menuChoice);
  152.         itemNr = LoWord (menuChoice);
  153.         if (menuID == MENU_Apple) {
  154.             DoApple (itemNr);
  155.         }
  156.     }
  157.  
  158.     HiliteMenu (0);
  159. } /*DoMenu*/
  160.  
  161. /*----------*/
  162. MenuHandle        menu;
  163. Boolean            menuBarChanged;
  164.  
  165. /*----------*/
  166. static void Enable    (short        itemNr,
  167.                      Boolean    enabled)
  168. {
  169.     if (enabled) {
  170.         EnableItem  (menu, itemNr);
  171.     } else {
  172.         DisableItem (menu, itemNr);
  173.     }
  174. } /*Enable*/
  175.  
  176. /*----------*/
  177. static void EnableTitle    (MenuHandle        menu,
  178.                          Boolean        enabled)
  179. {
  180.     if (enabled != ((**menu).enableFlags & 1)) {
  181.         menuBarChanged = true;
  182.     }
  183.     if (enabled) {
  184.         EnableItem  (menu, 0);
  185.     } else {
  186.         DisableItem (menu, 0);
  187.     }
  188. } /*EnableTitle*/
  189.  
  190. /*----------*/
  191. void    UpdateMenus (void)
  192. {
  193.     WindowPeek        frontPeek;
  194.     Boolean            isFront;        /*is there a front window?*/
  195.     Boolean            isCur;            /*is there a current window?*/
  196.     Boolean            isCurDoc;        /*is there a current document?*/
  197.     Boolean            isDirty;        /*is it dirty?*/
  198.     Boolean            hasFile;        /*does it have a file?*/
  199.     Boolean            isSelected;        /*is anything selected?*/
  200.     Boolean            isDesk;            /*is the front window a desk acc?*/
  201.     Boolean            isText;            /*is there a current text field?*/
  202.     Boolean            isScrap;        /*is there any scrap?*/
  203.     menuBarChanged = false;
  204.  
  205.     isFront        = (FrontWindow () != nil);
  206.     isCur        = (curWindow != nil);
  207.     isDirty        = false;
  208.     hasFile        = false;
  209.     isSelected    = false;
  210.     isCurDoc    = (cur->mDoc != nil);
  211.     if (isCurDoc) {
  212.         isDirty        = cur->mDoc->mEngine->IsDirty ();
  213.         hasFile        = cur->mDoc->mEngine->HasFile ();
  214.     }
  215.  
  216.     isDesk = false;
  217.     if (isFront) {
  218.         frontPeek    = (WindowPeek) FrontWindow ();
  219.         isDesk        = (frontPeek->windowKind < 0);
  220.     }
  221.  
  222.     TEHandle    curTE = cur->GetCurTE ();
  223.  
  224.     isText        = (curTE != nil);
  225.     isScrap        = false;
  226.     if (isText) {
  227.         isSelected    = ((**curTE).selStart != (**curTE).selEnd);
  228.         isScrap        = (TEGetScrapLength () > 0);
  229.     }
  230.  
  231.     menu = FileMenu;
  232.     Enable (cFileClose,        isFront);
  233.     Enable (cFileSave,        isDirty);
  234.     Enable (cFileSaveAs,    isCur);
  235.     Enable (cFileRevert,    isDirty);
  236.  
  237.     menu = EditMenu;
  238.     if (isFront) {
  239.         Enable (cEditUndo,        isDesk);
  240.         Enable (cEditCut,        isDesk || isSelected);
  241.         Enable (cEditCopy,        isDesk || isSelected);
  242.         Enable (cEditPaste,        isDesk || isScrap);
  243.         Enable (cEditClear,        isDesk || isSelected);
  244.         Enable (cEditSelectAll,    isText);
  245.  
  246.     }
  247.     EnableTitle (EditMenu,     isFront);
  248.  
  249.  
  250.     if (menuBarChanged) {
  251.         DrawMenuBar ();
  252.     }
  253. } /*UpdateMenus*/
  254.